﻿/**
 * Created by Teddy on 2014/6/5.
 */

LocalMobelInfo = function() {};
LocalMobelInfo.prototype.putStringData = function(key, value) {
    localStorage.setItem(key, value + "");
};
LocalMobelInfo.prototype.putBooleanData = function(key, value) {
    localStorage.setItem(key, value + "");
};
LocalMobelInfo.prototype.putIntData = function(key, value) {
    localStorage.setItem(key, value + "");
};
LocalMobelInfo.prototype.updateUser = function(name, password) {
    localStorage.setItem("XPP_USERNAME", name + "");
    localStorage.setItem("XPP_PASSWORD", password + "");
}
LocalMobelInfo.prototype.getBooleanData = function(key) {
    return "true" == localStorage.getItem(key) ? true : false;
};
LocalMobelInfo.prototype.getStringData = function(key) {
    return localStorage.getItem(key);
};
LocalMobelInfo.prototype.getIntData = function(key) {
    if (!isNullOrUndefined(localStorage.getItem(key))) {
        return parseInt(localStorage.getItem(key));
    } else {
        return 0;
    }
};
LocalMobelInfo.prototype.getUserName = function() {
    return localStorage.getItem("XPP_USERNAME");
}
LocalMobelInfo.prototype.getUserPassword = function() {
    return localStorage.getItem("XPP_PASSWORD");
}
var dthMobileOA = null;
var serviceUrl = "http://localhost:8080/cakephp/mobileUsersAdapter/";
function init() {
    //healthMagicBoxDataSet.openDatabase("healthMagicBoxLocalDB", "0.1", "HealthMagicBox mobileDB", 2 * 1024 * 1024);

    dthMobileOA = new LocalMobelInfo();
    var _username = dthMobileOA.getUserName();
    var _password = dthMobileOA.getUserPassword();
    $("#txtusername").val(_username);
    $("#txtpassword").val(_password);
    if (_password) {
        $("#recordPwd").prop("checked", true);
        // 自动登录
        if (dthMobileOA.getBooleanData("autologin")) {
            $("#autologin").prop("checked", true);
            loginFun();
        }
    }

}
$.ui.ready(function() {
    init();
    $("#btnLogin").bind("click", loginFun);
    //$("#logoutSure").bind("click", logoutSureFun);
    //$("#logoutCancel").bind("click", logoutCancelFun);
    $.ui.backButtonText = "返回";

});

function loginFun() {
    var para = {
        username: $("#txtusername").val(),
        password: $("#txtpassword").val()
    };
    var url = serviceUrl + "mobileuserlogin";
    //var url = $N.util.getServiceURL + "mobileuserlogin";
    $.ui.showMask("Login.... ....");
    $.post(url, para, function(res) {
        if (res.IsSuccess) {

            var isRecondPwd = $("#recordPwd").prop("checked");
            if (isRecondPwd) {
                dthMobileOA.updateUser($("#txtusername").val(), $("#txtpassword").val());
            } else {
                dthMobileOA.updateUser($("#txtusername").val(), null);
            }
            var isoutologin = $("#autologin").prop("checked");
            dthMobileOA.putBooleanData("autologin", isoutologin);
            window.location.href = "frame.html";
        } else {
            $.ui.hideMask();
            //alert(res.Msg);
            $("#afui").popup(res.Msg); //res.Msg
        }
    }, "json", function() {
        $.ui.hideMask();
        $("#afui").popup("网络连接失败！");
    })
}